package com.redrose.audioandvideo.utils;

import android.os.Build;
import android.util.Log;

import com.redrose.audioandvideo.BuildConfig;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 日志工具类
 */

public class LogUtils {

    private static final String TAG = "LogUtils";
    private static final boolean DEBUG = BuildConfig.DEBUG;
    private static final SimpleDateFormat LOGINFOFORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
    private static final SimpleDateFormat LOG_FILE_NAME = new SimpleDateFormat("yyyy-MM-dd");
    private static final String FILENAME = LOG_FILE_NAME.format(new Date()) + ".log";
    private static boolean isInit = false;
    static {


    }
    public static void init(){
        isInit = true;
        /** 创建App文件夹*/
        mkdirs(PathUtil.getAppPath());
        /** 创建日志文件夹*/
        mkdirs(PathUtil.getLogPath());
        /** 创建本次操作日志文件*/
        creatNewFile(PathUtil.getLogPath() + FILENAME);
    }
    public static void e(String tag, String msg) {
        if (DEBUG) {
            Log.e(tag, msg);
        }

        writeFile(tag + "\t" + msg);
    }

    public static void e(String tag, Throwable e) {
        if (DEBUG) {
            e.printStackTrace();
        }
    }

    public static void d(String tag, String msg) {
        if (DEBUG) {
            Log.d(tag, msg);
        }
        writeFile(tag + "\t" + msg);
    }

    /**
     * 日志写入文件
     */
    private static void writeFile(String msg) {
        try {
            FileWriter fileWriter = new FileWriter(PathUtil.getLogPath() + FILENAME, true);
            fileWriter.write(LOGINFOFORMAT.format(new Date()) + "\t");
            fileWriter.write(msg + "\n");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 写入设备信息
     */
    private static void writeFile() {
        try {
            FileWriter fileWriter = new FileWriter(PathUtil.getLogPath() + FILENAME, true);
            fileWriter.write("-------------------------------------------------------------" + "\n");
            fileWriter.write(LOGINFOFORMAT.format(new Date()) + "\n");
            String msg = String.format(Locale.getDefault(), "Brand: %s\nDeviceNumber: %s\nOS: %s\nVersionCode: %s\nVersionName: %s\n",
                    Build.BRAND, Build.MODEL, Build.VERSION.RELEASE, BuildConfig.VERSION_CODE, BuildConfig.VERSION_NAME);
            fileWriter.write(msg + "\n");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建文件夹
     */
    private static void mkdirs(String path) {
        Log.d(TAG," ------- path   " + path);
        mkdirs(new File(path));
    }

    /**
     * 创建文件夹及父目录
     */
    private static void mkdirs(File file) {
        if (file.exists() && file.isDirectory()) {
            return;
        }
        try {
            boolean result = file.mkdirs();
            String msg = result ? "文件夹创建成功" : "文件夹创建失败";
            Log.d(TAG, msg);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void creatNewFile(String fileName) {
        File file = new File(fileName);
        if (!file.exists()) {
            try {
                boolean result = file.createNewFile();
                String msg = result ? "创建日志成功" : "创建日志失败";
                Log.d(TAG, msg);
                /** 首次创建日志文件写入设备信息*/
                writeFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 清除日志文件
     */
    private static void cleanLogFile() {
        File file = new File(PathUtil.getLogPath());
        if (file.exists() && file.isDirectory()) {
            /** 虚拟机退出的时候才会执行*/
//            file.deleteOnExit();
            boolean result = file.delete();
            String msg = result ? "删除日志成功" : "删除日志失败";
            Log.d(TAG, msg);
        }
    }
}
